package admin;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import com.sincetimes.game.util.ConfigManager;
import com.sincetimes.game.util.WebUtil;

public class LoginView extends AbstractController{

	@Override
	protected ModelAndView handleRequestInternal(HttpServletRequest req,
			HttpServletResponse resp) throws Exception {
		String username=req.getParameter("username");
		String password=req.getParameter("password");
		if(username!=null&&password!=null&&!"".equals(username)&&!"".equals(password)){
			if("adminuser".equals(username)&&"dragonstat".equals(password)){
				String key=WebUtil.getMD5Code(username+password);
				Cookie coo=new Cookie("loginkey",key);
				coo.setMaxAge(60*60);
				resp.addCookie(coo);
				//req.setAttribute("username", username);
				//req.setAttribute("password", password);
				resp.sendRedirect(ConfigManager.getInstance().getConfig("resurl")+"/file.dragon");
			}else{
				req.setAttribute("errorinfo", "username or password is wrong");
				return new ModelAndView("login");
			}
		}
		return new ModelAndView("login");
	}

}
